Skip to content

fix(FOUR-34260): store per-user random TOTP secrets for 2FA - #9127

Open
gproly wants to merge 11 commits into
developfrom
bugfix/FOUR-34260
Open

gproly wants to merge 11 commits into
developfrom
bugfix/FOUR-34260

Conversation

@gproly

@gproly gproly commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Stop deriving 2FA secrets from public uuid and username, which allowed anyone with those fields to forge valid codes. Generate secrets with TOTP::generate(), persist encrypted on users.two_factor_secret, and add regression tests for predictable-code rejection and normal 2FA flow.

Existing Authenticator App enrollments must scan a new QR after deploy.

https://processmaker.atlassian.net/browse/FOUR-34260

ci:deploy

Stop deriving 2FA secrets from public uuid and username, which allowed
anyone with those fields to forge valid codes. Generate secrets with
TOTP::generate(), persist encrypted on users.two_factor_secret, and add
regression tests for predictable-code rejection and normal 2FA flow.

Existing Authenticator App enrollments must scan a new QR after deploy.

https://processmaker.atlassian.net/browse/FOUR-34260
@cursor

cursor Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

PR Summary

High Risk
Authentication and TOTP secret handling changed; existing authenticator enrollments must re-scan QR after deploy, and a DB migration is required.

Overview
Fixes FOUR-34260 by replacing predictable TOTP secrets (derived from uuid + username) with cryptographically random secrets persisted as encrypted two_factor_secret and two_factor_delivery_secret on users (new migration).

Authenticator app secrets stay in session as PENDING_AUTHENTICATOR_SECRET until QR enrollment and first successful 6-digit verification, then are saved; email/SMS use a separate delivery secret generated on first send. Validation now routes 6-digit codes through the app secret and 8-digit codes through delivery secrets only when those methods are enabled.

Login flow changes: users with authenticator-only 2FA and no stored secret are sent to the QR enrollment page first; QR and OTP views are refactored onto the shared auth layout. Regression feature and unit tests assert identity-derived codes are rejected while stored-secret codes still work.

Reviewed by Cursor Bugbot for commit 9353142. Bugbot is set up for automated code reviews on this repo. Configure here.

@julceslauhub julceslauhub left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approved

… users

Stop deriving 2FA codes from public uuid and username. Persist CSPRNG
secrets encrypted on users.two_factor_secret, add regression tests, and
on login route legacy users without a secret to QR (auth app only) or
force a fresh email/SMS code.

Run the migration on deploy. Authenticator App users must scan a new QR.

https://processmaker.atlassian.net/browse/FOUR-34260

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@nolanpro

Copy link
Copy Markdown
Contributor

QA server K8S was successfully deployed https://tenant-1.ci-acbfe4bfe2.engk8s.processmaker.net

- TwoFactorAuthNotification mail subject and body line
- TwoFactorAuthController invalid code errors

Fixes sync mail send hanging on /2fa when subject used _().

https://processmaker.atlassian.net/browse/FOUR-34260

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Agentic Security Review — 1 finding(s).

  • [HIGH] ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php:137 — A password-authenticated session that has not completed 2FA can GET /2fa/auth_app_qr. displayAuthAppQr only checks that a user is present, then generateQr calls getUserTotpSecret, which returns the stored two_factor_secret or mints one and saveQuietly()s it. That secret is placed in the otpauth provisioning URI and returned as the QR image. The same secret is used for email/SMS OTPs (sha512, 8 digits, 300s) and for authenticator codes (default 6-digit TOTP). validateCode treats any 6-character code as authenticator mode, does not consult getValid2FAPreferences, and the controller stores the result in 2fa-validated, which the 2fa middleware accepts as completion. This holds for email-only and SMS-only users and when a secret is already stored. The new redirect in displayTwoFactorAuthForm only sends authenticator-only users with an empty secret to this page; it does not authorize the route. Showing a QR once for that incomplete authenticator enrollment can stay; the route must not mint or re-show the secret for other methods or after a secret already exists, and a 6-digit code must be rejected unless Authenticator App is an enabled method.
    • Impact: An attacker who has the victim's password, or that pre-2FA session, recovers the long-term second-factor secret and can pass the 2FA gate without the victim's email, phone, or authenticator. two_factor_secret is now a confidential persisted credential rather than a value derived from public identity, but this path still discloses it to a session that has only passed primary authentication, and the shared secret unlocks email and SMS as well as authenticator.
    • Remediation: Allow displayAuthAppQr to mint and show a QR only when Authenticator App is an enabled method and enrollment is still incomplete. After two_factor_secret exists, do not return it again to a session that has not just completed enrollment; require a verified second factor before any later display. Keep the email/SMS OTP secret separate from the authenticator secret, and in validateCode accept a 6-digit authenticator code only when Authenticator App is one of that user's enabled methods.

@nolanpro

Copy link
Copy Markdown
Contributor

QA server K8S was successfully deployed https://tenant-1.ci-acbfe4bfe2.engk8s.processmaker.net

Refactor otp and auth_app_qr to extend auth.layouts.auth instead of legacy
standalone HTML, background assets, and inline CSS. Use existing skip-language-
selector and skip-auth-language-scripts sections so 2FA matches password-reset
branding without changing the auth layout.

https://processmaker.atlassian.net/browse/FOUR-34260
Use fresh() instead of User::refresh() on /2fa display so two_factor_secret
is read from the database without invalidating permissions cache.

https://processmaker.atlassian.net/browse/FOUR-34260

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Agentic Security Review — 1 finding(s).

  • [HIGH] ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php:137 — A password session that has not set 2fa-validated can GET /2fa/auth_app_qr and receive the durable TOTP seed in the provisioning QR. displayAuthAppQr only checks that a user is present, then generateQr embeds users.two_factor_secret in an otpauth URI. That same seed is what validateCode accepts: code length alone selects the 6-digit authenticator profile or the 8-digit sha512/300s email/SMS profile. After the seed exists, every later password login can read it again, and an email/SMS-only user can open the route directly even though authenticator is not their method.
    • Impact: This is the second-factor possession boundary. Password login already creates a session, and the 2fa middleware only wraps later application routes, so the pre-2FA session is the actor 2FA is meant to stop. The new stored secret is no longer recomputable from uuid and username, but this route still returns it, so a phished or reused password is enough to mint a valid code and set 2fa-validated. That includes users whose only methods are email or SMS, because their emailed or texted code is derived from the seed on the QR. The one-time authenticator enrollment redirect for an empty secret is the intended setup step; repeating the seed, or showing it when authenticator is not enabled, removes the second factor.
    • Remediation: Serve the provisioning QR only for initial authenticator enrollment: authenticator must be an enabled method for that user, and two_factor_secret must still be empty. Call generateQr only in that case, then require a confirming code from the app. After the seed is stored, deny /2fa/auth_app_qr to any session that has not already passed 2FA, including a direct request when the challenge-page link is hidden. Do not create or return the email/SMS seed from this route. Keep validateCode from accepting a profile the user's enabled methods do not allow, so a seed disclosed during authenticator setup cannot also satisfy an email or SMS challenge.

@nolanpro

Copy link
Copy Markdown
Contributor

QA server K8S was successfully deployed https://tenant-1.ci-acbfe4bfe2.engk8s.processmaker.net

…ment

Store email/SMS TOTP in two_factor_delivery_secret and authenticator TOTP in
two_factor_secret. Gate /2fa/auth_app_qr on enabled Authenticator App and empty
enrollment secret; reject six-digit codes unless the method is allowed.

Extend the users migration, tighten OTP link visibility, and add unit/feature
coverage for predictable-secret bypass and QR route access.

https://processmaker.atlassian.net/browse/FOUR-34260

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@nolanpro

Copy link
Copy Markdown
Contributor

QA server K8S was successfully deployed https://tenant-1.ci-acbfe4bfe2.engk8s.processmaker.net

Combine authenticator-method and existing-secret checks into one redirect
to /2fa so displayAuthAppQr stays within the return-statement limit.

https://processmaker.atlassian.net/browse/FOUR-34260
Extract authenticator QR redirect and send-code provisioning into private
helpers so Sonar S3776 stays within the limit.

https://processmaker.atlassian.net/browse/FOUR-34260
Accumulate validation in a $valid flag and return once so validateCode stays
within the return-statement limit without changing 2FA verification behavior.

https://processmaker.atlassian.net/browse/FOUR-34260
…id code

Defer persisting two_factor_secret until a six-digit code verifies. Store the
QR provisioning secret in session so enrollment can be retried without locking
users out of the authenticator link or QR page.

https://processmaker.atlassian.net/browse/FOUR-34260

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 0a72e9c. Configure here.

@nolanpro

Copy link
Copy Markdown
Contributor

QA server K8S was successfully deployed https://tenant-1.ci-acbfe4bfe2.engk8s.processmaker.net

Do not redirect authenticator-only users back to the QR page when
PENDING_AUTHENTICATOR_SECRET is in session, so they can enter a code after
scanning. Add a feature test that GET /2fa returns the OTP view after QR enrollment.

https://processmaker.atlassian.net/browse/FOUR-34260
@nolanpro

Copy link
Copy Markdown
Contributor

QA server K8S was successfully deployed https://tenant-1.ci-acbfe4bfe2.engk8s.processmaker.net

@julceslauhub
julceslauhub self-requested a review November 10, 2026 20:46
@decisions-sonarqube

Copy link
Copy Markdown

Quality Gate failed Quality Gate failed

Failed conditions
2 New issues
2 New Maintainability Issues (required ≤ 0)

See analysis details on SonarQube

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE SonarQube for IDE

@julceslauhub julceslauhub left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants